﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class rpt_MasterPODetails : DevExpress.XtraReports.UI.XtraReport
    {
        public string po_id;
        public rpt_MasterPODetails()
        {
            InitializeComponent();
            this.po_id = null;            
        }

        public void BindData()
        { 
            POID.DataBindings.Add("Text", DataSource, "POID");
            OrderNo.DataBindings.Add("Text", DataSource, "OrderNo");
            IQID.DataBindings.Add("Text", DataSource, "IQID");
            PartName.DataBindings.Add("Text", DataSource, "PartName");
            PartNo.DataBindings.Add("Text", DataSource, "PartNo");
            Maker.DataBindings.Add("Text", DataSource, "Maker");            
            Quantity.DataBindings.Add("Text", DataSource, "Quantity");
            UnitID.DataBindings.Add("Text", DataSource, "UnitID");
            Price.DataBindings.Add("Text", DataSource, "Price");
            Price.DataBindings["Text"].FormatString = "{0:n}";
            Amount.DataBindings.Add("Text", DataSource, "Amount");
            Amount.DataBindings["Text"].FormatString = "{0:n}";
            CurrencySymbol.DataBindings.Add("Text", DataSource, "CurrencyID");
            DeliveryTermName.DataBindings.Add("Text", DataSource, "DeliveryDate");
            Way.DataBindings.Add("Text", DataSource, "Way");
            PackingNote.DataBindings.Add("Text", DataSource, "PackingNote");            
            Note.DataBindings.Add("Text", DataSource, "Note"); 
        }

        public void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT TOP 100 PERCENT t1.[OrderNo], t1.[POID], t2.[IQID], t2.[PartEnglishName] AS PartName,"
                + " ISNULL(t2.PartNoPurchase, t2.PartNo) AS PartNo, t3.[EnglishName] AS Maker,"
                + " ROUND(t1.[Quantity], 1) AS Quantity, t1.[UnitID],"
                + " ROUND(t1.[UnitPrice], 2) AS Price, ROUND((t1.[Quantity] * t1.[UnitPrice]), 2) AS Amount,"
                + " t1.[CurrencyID], CONVERT(VARCHAR(10), t1.[DeliveryDate], 103) AS DeliveryDate,"
                + " t6.[TransportName] AS Way, t2.[PackingNote], t2.[Remark] AS [Note]"
                + " FROM [241_MasterPODetails] AS t1 LEFT JOIN"
                + " [222_Part] AS t2 ON t1.[PartNo]=t2.[PartNo] LEFT JOIN"
                + " [222_Maker] AS t3 ON t2.[MakerID]=t3.[MakerID] LEFT JOIN"                
                + " [222_DeliveryTerm] AS t5 ON t1.[DeliveryTermID]=t5.[DeliveryTermID] LEFT JOIN"
                + " [222_Transport] AS t6 ON t1.[TransportID]=t6.[TransportID]"
                + " WHERE t1.POID='" + po_id + "' AND t1.Cancelled=0 AND t1.Change=0";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {                
                try
                {
                    foreach (DataRow result_row in sourceDataSet.Tables[0].Rows)
                    {
                        if (result_row["UnitID"].ToString().Trim().Length > 0)
                        {
                            if (((int)(result_row["UnitID"].ToString().Trim()[0])) == 65402)
                            {
                                result_row.BeginEdit();
                                result_row["UnitID"] = "PC";
                                result_row.EndEdit();
                            }
                        }
                    }

                    this.DataSource = sourceDataSet.Tables[0];
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }
    }
}
